package com.example.practice.creative;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ListView;

import com.example.practice.BaseActivity;
import com.example.practice.R;
import com.example.practice.bean.SensorValue;

public class CreativeActivity extends BaseActivity {
	private BuyerFragment buyerFragment;
	private SellerFragment sellerFragment;
	private static List<String> items = new ArrayList<String>();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_creative);

		sellerFragment = new SellerFragment();
		buyerFragment = new BuyerFragment();
		SPUtil.init(this);
		items.clear();
		items.add("苹果");
		items.add("西瓜");
		items.add("鸭梨");
		items.add("龙眼");
		items.add("荔枝");
		
		if (savedInstanceState == null) {
			getSupportFragmentManager().beginTransaction()
					.add(R.id.container, sellerFragment).commit();
		}
		
		findViewById(R.id.btn_buy).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				getSupportFragmentManager().beginTransaction()
				.replace(R.id.container, buyerFragment).commit();
			}
		});
		
		findViewById(R.id.btn_sell).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				getSupportFragmentManager().beginTransaction()
				.replace(R.id.container, sellerFragment).commit();
			}
		});
	}

	


	public static class BuyerFragment extends Fragment {
		private ListView listView;
		public BuyerFragment(){
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_buyer, container,
					false);
			listView = (ListView)rootView.findViewById(R.id.list_buyer);
			listView.setAdapter(new BuyerAdapter(items));
			return rootView;
		}
		
	}
	
	public static class SellerFragment extends Fragment {
		private ListView listView;
		public SellerFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_seller, container,
					false);
			listView = (ListView)rootView.findViewById(R.id.list_seller);
			listView.setAdapter(new SellerAdapter(items));
			return rootView;
		}
		
	
	}

	@Override
	protected void onGetSensorValue(SensorValue sensorValue) {
		// TODO Auto-generated method stub
		
	}

}
